part of '../repository.dart';

const _kEmailVerificationCodeEmailKey = 'email';
const _kEmailVerificationCodeValueKey = 'value';
const _kEmailVerificationCodeExpirationTimeKey = 'expiration_time';
const _kEmailVerificationCodeRemainingAttemptsKey = 'remaining_attempts';

class _EmailVerificationCodeDto extends Equatable {
  const _EmailVerificationCodeDto._internal({
    required this.email,
    required this.value,
    required this.expirationTimeDto,
    required this.remainingAttempts,
  });

  final String email;
  final String value;
  final _TimeDto expirationTimeDto;
  final int remainingAttempts;

  @override
  List<Object?> get props {
    return [
      email,
      value,
      expirationTimeDto,
      remainingAttempts,
    ];
  }

  factory _EmailVerificationCodeDto.fromJson(Map<String, dynamic> json) {
    final expirationTimeString = json[_kEmailVerificationCodeExpirationTimeKey] as String;
    final expirationTimeDto = _TimeDto.fromString(expirationTimeString);

    return _EmailVerificationCodeDto._internal(
      email: json[_kEmailVerificationCodeEmailKey],
      value: json[_kEmailVerificationCodeValueKey],
      expirationTimeDto: expirationTimeDto,
      remainingAttempts: json[_kEmailVerificationCodeRemainingAttemptsKey],
    );
  }

  factory _EmailVerificationCodeDto.fromEntity(EmailVerificationCode emailVerificationCode) {
    final expirationTimeDto = _TimeDto.fromEntity(emailVerificationCode.expirationTime);

    return _EmailVerificationCodeDto._internal(
      email: emailVerificationCode.email,
      value: emailVerificationCode.value,
      expirationTimeDto: expirationTimeDto,
      remainingAttempts: emailVerificationCode.remainingAttempts,
    );
  }

  EmailVerificationCode toEntity() {
    return EmailVerificationCode(
      email: email,
      value: value,
      expirationTime: expirationTimeDto.toEntity(),
      remainingAttempts: remainingAttempts,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kEmailVerificationCodeEmailKey: email,
      _kEmailVerificationCodeValueKey: value,
      _kEmailVerificationCodeExpirationTimeKey: expirationTimeDto.toString(),
      _kEmailVerificationCodeRemainingAttemptsKey: remainingAttempts,
    };
  }
}
